﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BangleCMS.Model;
using BangleCMS.BLL;
using System.Text;
using System.Linq;

public partial class System_Page_WebContentModify : BasePage
{
    t_webnavBLL nav_bll = new t_webnavBLL();

    #region 属性

    /// <summary>
    /// Action
    /// </summary>
    private string PageCode
    {
        get
        {
            return Request.QueryString["PageCode"] == null ? "" : Request.QueryString["PageCode"];
        }
    }

    public string TreeHtmlString
    {
        get
        {
            return ViewState["TreeHtmlString"] == null ? "" : ViewState["TreeHtmlString"].ToString();
        }
        set
        {
            ViewState["TreeHtmlString"] = value;
        }
    }
    #endregion

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //检验用户权限
        base.CheckUserPermission("003011");

        if (!IsPostBack)
        {
            BindWebNavTree();
            getDate();
        }
    }
    #endregion

    #region 加载数据
    private void BindWebNavTree()
    {
        List<t_webnav> list;
        list = nav_bll.GetModelList(" Valid=1 and IsSys=0 ");

        TreeNode root = new TreeNode();
        root.Text = "网站菜单目录";
        root.Value = "";
        root.NavigateUrl = "javascript:void(0)";
        foreach (t_webnav item in list.Where(o => (o.IsSys == 0 && o.ParentId == 0)))
        {
            TreeNode node = new TreeNode();
            node.Text = item.Title;
            node.Value = item.Code;
            node.NavigateUrl = "WebContentModify.aspx?PageCode=" + item.Code;
            if (item.Code == PageCode && PageCode != "")
            {
                node.Selected = true;
            }
            foreach (t_webnav cItem in list.Where(o => o.ParentId == item.WebNavId))
            {
                TreeNode cNode = new TreeNode();
                cNode.Text = cItem.Title;
                cNode.Value = cItem.Code;
                cNode.NavigateUrl = "WebContentModify.aspx?PageCode=" + cItem.Code;
                if (cItem.Code == PageCode && PageCode != "")
                {
                    cNode.Selected = true;
                }

                node.ChildNodes.Add(cNode);
                node.NavigateUrl = "javascript:void(0)";
            }
            root.ChildNodes.Add(node);
        }

        this.tv_WebNav.Nodes.Add(root);
    }
    private void getDate()
    {
        this.Editor.Text = "";
        if (PageCode == "")
        {
            this.Editor.Visible = false;
            this.lab_PageName.Text = "请选择要更新的页面";
            this.lab_Title.Text = "请选择要更新的页面";
            return;
        }
        try
        {
            t_page model = new t_page();
            t_pageBLL bll = new t_pageBLL();
            model = bll.GetModel(PageCode);
            if (model == null)
            {
                return;
            }
            this.lab_PageName.Text = model.PageName;
            this.lab_Title.Text = model.Title;
            this.Editor.Text = model.Content;
        }
        catch (Exception ex)
        {
        }
    }
    #endregion

    #region 提交
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        #region 修改
        try
        {
            t_page model = new t_page();
            t_pageBLL bll = new t_pageBLL();

            model = bll.GetModel(PageCode);

            if (model == null)
            {
                model = new t_page();
            }

            model.Content = this.Editor.Text;
            model.UpdateUser = base.CurrentSession.UserID;
            model.UpdateDate = DateTime.Now;
            model.Valid = 1;

            if (model.PageId < 1)
            {
                model.Title = this.lab_Title.Text;
                model.PageCode = PageCode;
                model.PageName = model.Title;
                model.CreateDate = model.UpdateDate;
                model.CreateUser = model.UpdateUser;
                bll.Add(model);
            }
            else
            {
                bll.Update(model);
            }

            bll.Update(model);

            getDate();
            base.Alert("操作成功！");
        }
        catch (Exception ex)
        {
            base.Alert("操作失败！");
            //throw ex;
        }
        #endregion
    }
    #endregion

}
